'use strict';//上传图片
const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');
class UploadController extends Controller {
    async uploadImg() {
        const { ctx, app } = this;
        const file = ctx.request.files[0];
        
        // let formname = ctx.request.query; 
        if (file) {
            file.filename = Math.round(Math.random() * 100 + 1) + Date.now() + path.extname(file.filename);
            const pathName = 'app/public/img/' + file.filename;
            const _path = 'public/img/' + file.filename;
            fs.writeFileSync(pathName, fs.readFileSync(file.filepath));
            const data = await app.mysql.insert('picture', {
                
                img: pathName
            })
            // console.log(data);
            ctx.body = {
                code: 0,
                msg: '上传成功',
                data: _path
            }
        } else {
            ctx.body = {
                code: 4001,
                msg: '上传失败'
            }
        }
    }
}
module.exports = UploadController;